import json
import random
import string
from typing import Dict, Any

def generate_random_string(length: int = 8) -> str:
    """Generate a random string of specified length."""
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))

def generate_random_user_data() -> Dict[str, Any]:
    """Generate random user data for testing."""
    return {
        "loginName": f"test_user_{generate_random_string()}",
        "userName": f"Test User {generate_random_string()}",
        "password": "Test@123",
        "email": f"test_{generate_random_string()}@example.com",
        "phone": f"1{generate_random_string(10)}",
        "status": 0,
        "userType": "ADMIN"
    }

def generate_random_role_data() -> Dict[str, Any]:
    """Generate random role data for testing."""
    return {
        "roleName": f"Test Role {generate_random_string()}",
        "roleKey": f"TEST_ROLE_{generate_random_string().upper()}",
        "roleSort": random.randint(1, 100),
        "status": 0,
        "menuIds": []
    }

def generate_random_menu_data() -> Dict[str, Any]:
    """Generate random menu data for testing."""
    return {
        "menuName": f"Test Menu {generate_random_string()}",
        "parentId": 0,
        "orderNum": random.randint(1, 100),
        "path": f"/test/{generate_random_string().lower()}",
        "component": "TestComponent",
        "query": None,
        "isFrame": 0,
        "isCache": 0,
        "menuType": "M",
        "visible": "0",
        "status": "0",
        "perms": None,
        "icon": "test-icon"
    }

def assert_response_success(response) -> None:
    """Assert that the API response indicates success."""
    assert response.status_code == 200
    data = response.json()
    assert data["code"] == 200
    assert "data" in data

def assert_response_error(response, expected_code: int) -> None:
    """Assert that the API response indicates an error with the expected code."""
    assert response.status_code == 200
    data = response.json()
    assert data["code"] == expected_code
    assert "message" in data

def load_test_data(file_path: str) -> Dict[str, Any]:
    """Load test data from a JSON file."""
    with open(file_path, 'r', encoding='utf-8') as f:
        return json.load(f)

def save_test_data(file_path: str, data: Dict[str, Any]) -> None:
    """Save test data to a JSON file."""
    with open(file_path, 'w', encoding='utf-8') as f:
        json.dump(data, f, indent=2, ensure_ascii=False) 